<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="bg-container" />
      <div class="title-container">
        <h2 class="">视频物联网脆弱性检测系统中台(Panel)</h2>
      </div>
      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="用户名"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="密码"
          name="password"
          tabindex="2"
          auto-complete="on"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon
            :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
          />
        </span>
      </el-form-item>
      <div class="verification-code">
        <el-form-item prop="verification">
          <span class="svg-container">
            <svg-icon icon-class="svg-container" />
          </span>
          <el-input
            ref="verification-code"
            v-model="loginForm.verification"
            placeholder="验证码"
            name="verification-code"
            type="text"
            tabindex="1"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
        </el-form-item>
        <div class="refresh">
          <canvas id="canvas" width="100px" height="45px" />
          <a id="changeImg" @click="refresh">看不清，换一张</a>
        </div>
      </div>

      <el-button
        :loading="loading"
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        class="btn btn-primary btn-ghost btn-shine"
        @click.native.prevent="handleLogin"
        >登录</el-button
      >

      <div class="tips">
      </div>
    </el-form>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import { drawPic, refreshPic } from '@/utils/verification-code.js'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码长度不能小于6位'))
      } else {
        callback()
      }
    }
    const verificate = (rule, value, callback) => {
      if (
        value.toLocaleLowerCase() !== this.verificateCode.toLocaleLowerCase()
      ) {
        callback(new Error('验证码错误'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: 'admin',
        password: 'admin123',
        verification: '',
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', validator: validateUsername },
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword },
        ],
        verification: [
          { required: true, trigger: 'blur', validator: verificate },
        ],
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      verificateCode: '',
    }
    return {
      testvalue: '',
    }
    return {
      home: require('@/assets/404_images/home.png'),
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true,
    },
  },
  mounted() {
    this.verificateCode = drawPic()
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    refresh(e) {
      this.verificateCode = refreshPic(e)
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || '/' })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          this.verificateCode = drawPic()
          return false
        }
      })
    },
  },
}
</script>
<style lang="scss">
.login-container {
  .el-input__inner {
    background: transparent !important;
    border: 0px;
    color: #c6c9d0;
  }
}
</style>
<style lang="scss" scoped>
$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.555);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }

  .btn {
    --hue: 190;
    position: relative;
    padding: 1rem 3rem;
    font-size: 1rem;
    line-height: 1;
    color: white;
    text-decoration: none;
    text-transform: uppercase;
    background-color: hsl(var(--hue), 100%, 41%);
    border: 1px solid hsl(var(--hue), 100%, 41%);
    outline: transparent;
    overflow: hidden;
    cursor: pointer;
    user-select: none;
    white-space: nowrap;
    transition: 0.25s;
  }
  .btn:hover {
    background: hsl(var(--hue), 100%, 31%);
  }
  .btn-primary {
    --hue: 187;
  }
  .btn-ghost {
    color: hsl(var(--hue), 100%, 41%);
    background-color: transparent;
    border-color: hsl(var(--hue), 100%, 41%);
  }
  .btn-ghost:hover {
    color: white;
  }
  .btn-shine {
    color: white;
  }
  .btn-shine::before {
    position: absolute;
    content: '';
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(
      120deg,
      transparent,
      hsla(var(--hue), 100%, 41%, 0.5),
      transparent
    );
    transform: translateX(-100%);
    transition: 0.6s;
  }
  .btn-shine:hover {
    background: transparent;
    box-shadow: 0 0 20px 10px hsla(var(--hue), 100%, 41%, 0.5);
  }
  .btn-shine:hover::before {
    transform: translateX(100%);
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-image: url('../../assets/404_images/home.png');
  background-size: 100% 100%;
  overflow: hidden;

  .bg-container {
    position: absolute;
    width: 600px;
    height: 500px;
    background: rgba(54, 53, 53, 0.74);
    margin: -30px;
  }

  h2 {
    color: white;
  }

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 30px;
    margin: 0 auto;
    overflow: hidden;
  }
  .verification-code {
    width: 220px;

    .refresh {
      position: absolute;
      right: 35px;
      top: 383px;
      left: 280px;
    }

    a {
      font-size: 11px;
      color: #eee;
      position: relative;
      top: -18px;
      left: 20px;
    }
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    position: relative;
    top: 4px;
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 14px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
